//
//  QZRefreshHeaderView.m
//  QZRefreshData
//
//  Created by 弘德集团 on 2018/4/12.
//  Copyright © 2018年 弘德集团. All rights reserved.
//

#import "QZRefreshHeaderView.h"
#import "UIView+Frame.h"
#import "UIImage+animatedGIF.h"
@implementation QZRefreshHeaderView
{
    BOOL _hasLayoutedForManuallyRefreshing;
//    UIImageView *_gifImageView;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.textForNormalState = @"下拉可以加载最新数据";
        self.stateIndicatorViewNormalTransformAngle = 0;
        self.stateIndicatorViewWillRefreshStateTransformAngle = M_PI;
        [self setRefreshState:QZRefreshViewStateNormal];
        
//        NSURL *url = [[NSBundle mainBundle] URLForResource:@"feizhu_pull_refresh" withExtension:@"gif"];
//        UIImageView *image = [UIImageView new];
//        image.image = [UIImage animatedImageWithAnimatedGIFURL:url];
//        _gifImageView = image;
//        [self addSubview:image];
    }
    return self;
    
}

- (CGFloat)yOfCenterPoint
{
    return  - (self.height * 0.5);
}

- (void)didMoveToSuperview
{
    [super didMoveToSuperview];
    self.scrollViewEdgeinsets = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.center = CGPointMake(self.scrollView.width * 0.5, [self yOfCenterPoint]);
    
    // 模拟手动刷新
    if (self.isManuallyRefreshing && !_hasLayoutedForManuallyRefreshing && self.scrollView.contentInset.top > 0) {
        self.activityIndicatorView.hidden = NO;
        
        // 模拟下拉操作7
        CGPoint temp = self.scrollView.contentOffset;
        temp.y -= self.height * 2;
        self.scrollView.contentOffset = temp; // 触发准备刷新
        temp.y += self.height;
        self.scrollView.contentOffset = temp; // 触发刷新
        
        _hasLayoutedForManuallyRefreshing = YES;
    } else {
        self.activityIndicatorView.hidden = !self.isManuallyRefreshing;
    }
}

- (void)autoRefreshWhenViewDidAppear
{
    self.isManuallyRefreshing = YES;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if (![keyPath isEqualToString:QZRefreshViewObservingKeyPath] || self.refreshState == QZRefreshViewStateRefreshing) return;
    
    CGFloat y = [change[@"new"] CGPointValue].y;
    CGFloat criticalY = -self.height - self.scrollView.contentInset.top;
    
    // 只有在 y<=0 以及 scrollview的高度不为0 时才判断
    if ((y > 0) || (self.scrollView.bounds.size.height == 0)) return;
    
    // 触发SDRefreshViewStateRefreshing状态
    if (y >= criticalY && (self.refreshState == QZRefreshViewStateWillRefresh) && !self.scrollView.isDragging) {
        [self setRefreshState:QZRefreshViewStateRefreshing];
    }
    
    // 触发SDRefreshViewStateWillRefresh状态
    if (y < criticalY && (QZRefreshViewStateNormal == self.refreshState)) {
        [self setRefreshState:QZRefreshViewStateWillRefresh];
    } else if (y >= criticalY && self.scrollView.isDragging && self.refreshState != QZRefreshViewStateNormal) {
        self.refreshState = QZRefreshViewStateNormal;
    }
    
    if (self.refreshState == QZRefreshViewStateNormal) {
        CGFloat scale = (-y - self.scrollView.contentInset.top) / self.height;
        
        if ([self.delegate respondsToSelector:@selector(refreshView:didBecomeNormalStateWithMovingProgress:)]) {
            [self.delegate refreshView:self didBecomeNormalStateWithMovingProgress:scale];
        }
        
        if (self.normalStateOperationBlock) {
            self.normalStateOperationBlock(self, scale);
        }
    }
}

@end
